const util = require("../../utils/util");
var app = getApp();
Page({
    data: {
        userInfo: app.globalData.userInfo
    },

    //页面加载
    onLoad: function () {
        var that = this;
        //检查token是否存在
        var userInfo = util.getStorageSync("userInfo");
        if (userInfo != null) {
            app.globalData.userInfo = userInfo;
            console.log('存在userInfo信息');
            setTimeout(function () {
                wx.reLaunch({
                    url: '/pages/home/home'
                });
            }, 2000);
            return;
        }
        console.log('不存在userInfo信息');
        wx.login({
            success: function (res) {
                if (res.code) {
                    console.log('发起登录请求');
                    wx.request({
                        url: encodeURI(app.globalData.api + '/Account/CheckUserInfoByCode'),
                        data: {
                            code: res.code
                        },
                        method: 'GET',
                        success: function (res) {
                            if (res.statusCode != 200) {
                                console.log('网络请求失败，请重新授权');
                            } else if (res.data.resultCode == 0) {
                                app.globalData.userInfo = res.data.data;
                                util.setStorageSync("userInfo", app.globalData.userInfo, 2 * 60 * 60);
                            }
                        },
                        fail: function (res) {
                            console.log('请求错误' + res);
                        },
                        complete: function () {
                            console.log('发起请求结束');
                            wx.reLaunch({
                                url: '/pages/home/home'
                            });
                        }
                    });
                } else {
                    wx.reLaunch({
                        url: '/pages/home/home'
                    });
                }
            },
            fail: function () {
                console.log('登录失败');
                wx.reLaunch({
                    url: '/pages/home/home'
                });
            },
        });
    }
});